package com.baozhun.mall.common.widget.dialog

import android.content.Context
import android.view.View
import com.baozhun.mall.common.R
import com.baozhun.mall.common.base.mAppViewModel
import com.baozhun.mall.common.databinding.DialogChoosePhotoBinding
import com.baozhun.mall.common.listener.ViewOnClickListener
import com.baozhun.mall.common.util.CacheUtil
import com.baozhun.mall.common.util.GlideEngine
import com.baozhun.mall.common.util.UploadFileHelper
import com.blankj.utilcode.util.ToastUtils
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import java.io.File


/**
 * @Title：选择照片
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2022/11/21
 * @Version：1.0.0
 */
class ChoosePhotoDialog(context: Context, var uploadSuccessBack: (url:String) -> Unit) : BaseBottomPopupView<DialogChoosePhotoBinding>(context),
    ViewOnClickListener {

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_choose_photo
    }

    override fun onCreate() {
        super.onCreate()

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_cancel -> {
                dismiss()
            }
            R.id.tv_take_picture -> {
                dismiss()
                PictureSelector.create(context)
                    .openCamera(SelectMimeType.ofImage())
                    .forResultActivity(object : OnResultCallbackListener<LocalMedia?> {
                        override fun onResult(result: ArrayList<LocalMedia?>?) {
                            uploadPic(result)
                        }

                        override fun onCancel() {}
                    })
            }
            R.id.tv_select_from_album -> {
                dismiss()
                PictureSelector.create(context)
                    .openGallery(SelectMimeType.ofImage())
                    .setMaxSelectNum(1)
                    .setImageEngine(GlideEngine.createGlideEngine())
                    .forResult(object : OnResultCallbackListener<LocalMedia?> {
                        override fun onResult(result: ArrayList<LocalMedia?>?) {
                            uploadPic(result)
                        }

                        override fun onCancel() {}
                    })
            }

        }
    }

    /**
     * 上传图片
     */
    fun uploadPic(picList: ArrayList<LocalMedia?>?) {
        picList?.let { it ->
            var filePath = it.map { it?.realPath ?: "" }
            if (filePath.any { it.isNotEmpty() }) {
                UploadFileHelper.getInstance().uploadSingleFile(mAppViewModel, File(filePath[0])) {
                    uploadSuccessBack.invoke(it)
                    ToastUtils.showShort(com.baozhun.mall.res.R.string.upload_success)
                }
            } else {
                ToastUtils.showShort(com.baozhun.mall.res.R.string.pic_choose_exception_hint)
            }
        }
    }
}